#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/jiffies.h>
#include <linux/i2c.h>
#include <linux/mutex.h>
#include <linux/fs.h>
#include <asm/uaccess.h>

// 1、首先定义出入口和出口函数，定义一个结构体 i2c_driver at24cxx_driver 包括driver->name、attach_adapter和detach_client三个成员，在at24cxx_init中使用i2c_add_driver注册该驱动，在at24cxx_ exit中使用i2c_del_driver卸载该驱动。
// 代码如下：
static struct i2c_driver at24cxx_driver = {
	.driver = {
		.name = "at24cxx",
	},
	.attach_adapter = at24cxx_attach,
	.detach_client = at24cxx_detach,
};

static int at24cxx_init(void)
{
	i2c_add_driver(&at24cxx_driver);
	return 0;
}

static void at24cxx_exit(void)
{
	i2c_del_driver(&at24cxx_driver);
}

<<<<<<< HEAD
=======
module_init(at24cxx_init);
module_exit(at24cxx_exit);

MODULE_LICENSE("GPL");

>>>>>>> 17c977e6349bfc976796a3b9919e4dfc54798d66
// 2、具体实现at24cxx_attach函数。执行i2c_add_driver(&at24cxx_driver)后会，如果内核中已经注册了i2c适配器，则顺序调用这些适配器来连接我们的i2c设备，此过程是通过调用i2c_driver中的attach_adapter方法完成的。代码如下：

static unsigned short ignore[] = { I2C_CLIENT_END };
static unsigned short normal_addr[] = { 0x50, I2C_CLIENT_END }; /* 地址值是7位 */

static struct i2c_client_address_data addr_data = {
<<<<<<< HEAD
	.normal_i2c = normal_addr, /* 要发出S信号和设备地址并得到ACK信号,才能确定存在这个设备 */
	.probe = ignore,
	.ignore = ignore,
};
static int at24cxx_attach(struct i2c_adapter *adapter)
{
	return i2c_probe(adapter, &addr_data, at24cxx_detect);
=======
.normal_i2c = normal_addr, /* 要发出S信号和设备地址并得到ACK信号,才能确定存在这个设备 */
.probe = ignore,
.ignore = ignore,
};
static int at24cxx_attach(struct i2c_adapter *adapter)
{
return i2c_probe(adapter, &addr_data, at24cxx_detect);
>>>>>>> 17c977e6349bfc976796a3b9919e4dfc54798d66
}

// 3、具体实现at24cxx_detect函数，在at24cxx_attach函数中，调用i2c_probe函数，i2c_probe探测到设备后，调用at24cxx_detect函数，并把探测的地址作为参数输入。在 at24cxx_detect函数中，构造一个i2c_client结构体用于收发I2C数据，调用i2c_attach_client将client和adapter关联！然后注册字符驱动设备，用于读写IIC数据。

struct i2c_client *at24cxx_client;

static int major;
static struct class *cls;

static struct file_operations at24cxx_fops = {
<<<<<<< HEAD
	.owner = THIS_MODULE,
	.read = at24cxx_read,
	.write = at24cxx_write,
=======
.owner = THIS_MODULE,
.read = at24cxx_read,
.write = at24cxx_write,
>>>>>>> 17c977e6349bfc976796a3b9919e4dfc54798d66
};

static int at24cxx_detect(struct i2c_adapter *adapter, int address, int kind)
{
<<<<<<< HEAD
	printk("at24cxx_detect\n");

	at24cxx_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
	at24cxx_client->addr = address;
	at24cxx_client->adapter = adapter;
	at24cxx_client->driver = &at24cxx_driver;
	strcpy(at24cxx_client->name, "at24cxx");
	i2c_attach_client(at24cxx_client);

	major = register_chrdev(0, "at24cxx", &at24cxx_fops);

	cls = class_create(THIS_MODULE, "at24cxx");
	class_device_create(cls, NULL, MKDEV(major, 0), NULL, "at24cxx"); /* /dev/at24cxx */

	return 0;
=======
printk("at24cxx_detect\n");


at24cxx_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
at24cxx_client->addr = address;
at24cxx_client->adapter = adapter;
at24cxx_client->driver = &at24cxx_driver;
strcpy(at24cxx_client->name, "at24cxx");
i2c_attach_client(at24cxx_client);

major = register_chrdev(0, "at24cxx", &at24cxx_fops);

cls = class_create(THIS_MODULE, "at24cxx");
class_device_create(cls, NULL, MKDEV(major, 0), NULL, "at24cxx"); /* /dev/at24cxx */

return 0;
>>>>>>> 17c977e6349bfc976796a3b9919e4dfc54798d66
}

// 4、剩下的就是具体实现at24cxx_write和at24cxx_read了！
// 在at24cxx_write中：
// 使用copy_from_user(val, buf, 2)获得用户空间传入的要写入的寄存器地址和寄存器数据。构造一个写消息，通过i2c_transfer()函数完成消息的传递，最终写入相应寄存器数值。
// 在at24cxx_read中
// 使用copy_from_user(&address, buf, 1);获得用户空间传入的要读出的寄存器地址，构造一个读消息，一个写消息，通过i2c_transfer()函数完成消息的传递，读出相应寄存器数值，通过copy_to_user(buf, &data, 1)发送给应用层
static ssize_t at24cxx_write(struct file *file, const char __user *buf, size_t size, loff_t *offset)
{
<<<<<<< HEAD
	unsigned char val[2];
	struct i2c_msg msg[1];
	int ret;

	if (size != 2)
	return -EINVAL;

	copy_from_user(val, buf, 2);

	/* 数据传输三要素: 源,目的,长度 */
	msg[0].addr = at24cxx_client->addr; /* 目的 */
	msg[0].buf = val; /* 源 */
	msg[0].len = 2; /* 地址+数据=2 byte */
	msg[0].flags = 0; /* 表示写 */

	ret = i2c_transfer(at24cxx_client->adapter, msg, 1);
	if (ret == 1)
	return 2;
	else
	return -EIO;
=======
unsigned char val[2];
struct i2c_msg msg[1];
int ret;

if (size != 2)
return -EINVAL;

copy_from_user(val, buf, 2);

/* 数据传输三要素: 源,目的,长度 */
msg[0].addr = at24cxx_client->addr; /* 目的 */
msg[0].buf = val; /* 源 */
msg[0].len = 2; /* 地址+数据=2 byte */
msg[0].flags = 0; /* 表示写 */

ret = i2c_transfer(at24cxx_client->adapter, msg, 1);
if (ret == 1)
return 2;
else
return -EIO;
>>>>>>> 17c977e6349bfc976796a3b9919e4dfc54798d66
}


static ssize_t at24cxx_read(struct file *file, char __user *buf, size_t size, loff_t * offset)
{
<<<<<<< HEAD
	unsigned char address;
	unsigned char data;
	struct i2c_msg msg[2];
	int ret;

	/* address = buf[0]  
	* data = buf[1]
	*/
	if (size != 1)
	return -EINVAL;

	copy_from_user(&address, buf, 1);

	/* 数据传输三要素: 源,目的,长度 */

	/* 读AT24CXX时,要先把要读的存储空间的地址发给它 */
	msg[0].addr = at24cxx_client->addr; /* 目的 */
	msg[0].buf = &address; /* 源 */
	msg[0].len = 1; /* 地址=1 byte */
	msg[0].flags = 0; /* 表示写 */

	/* 然后启动读操作 */
	msg[1].addr = at24cxx_client->addr; /* 源 */
	msg[1].buf = &data; /* 目的 */
	msg[1].len = 1; /* 数据=1 byte */
	msg[1].flags = I2C_M_RD; /* 表示读 */


	ret = i2c_transfer(at24cxx_client->adapter, msg, 2);
	if (ret == 2)
	{
		copy_to_user(buf, &data, 1);
		return 1;
	}
	else
	return -EIO;
}
// 5、at24cxx_detach是调用内核中注册的适配器来断开我们注册过的i2c设备。
static int at24cxx_detach(struct i2c_client *client)
{
	printk("at24cxx_detach\n");
	class_device_destroy(cls, MKDEV(major, 0));
	class_destroy(cls);
	unregister_chrdev(major, "at24cxx");

	i2c_detach_client(client);
	kfree(i2c_get_clientdata(client));

	return 0;
}

module_init(at24cxx_init);
module_exit(at24cxx_exit);

MODULE_LICENSE("GPL");

=======
unsigned char address;
unsigned char data;
struct i2c_msg msg[2];
int ret;

/* address = buf[0]  
* data = buf[1]
*/
if (size != 1)
return -EINVAL;

copy_from_user(&address, buf, 1);

/* 数据传输三要素: 源,目的,长度 */

/* 读AT24CXX时,要先把要读的存储空间的地址发给它 */
msg[0].addr = at24cxx_client->addr; /* 目的 */
msg[0].buf = &address; /* 源 */
msg[0].len = 1; /* 地址=1 byte */
msg[0].flags = 0; /* 表示写 */

/* 然后启动读操作 */
msg[1].addr = at24cxx_client->addr; /* 源 */
msg[1].buf = &data; /* 目的 */
msg[1].len = 1; /* 数据=1 byte */
msg[1].flags = I2C_M_RD; /* 表示读 */


ret = i2c_transfer(at24cxx_client->adapter, msg, 2);
if (ret == 2)
{
copy_to_user(buf, &data, 1);
return 1;
}
else
return -EIO;
}
5、at24cxx_detach是调用内核中注册的适配器来断开我们注册过的i2c设备。
static int at24cxx_detach(struct i2c_client *client)
{
printk("at24cxx_detach\n");
class_device_destroy(cls, MKDEV(major, 0));
class_destroy(cls);
unregister_chrdev(major, "at24cxx");

i2c_detach_client(client);
kfree(i2c_get_clientdata(client));

return 0;
}


// 6.应用程序
// 首先使用fd = open("/dev/at24cxx", O_RDWR)打开at24cxx设备文件，通过传入的参数“r”“w”来判断是读寄存器还是写寄存器，如果是读寄存器，则调用read(fd, buf, 1)完成读取，如果是写寄存器，则调用write(fd, buf, 2)完成写入。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


/* i2c_test r addr
 * i2c_test w addr val
 */

void print_usage(char *file)
{
printf("%s r addr\n", file);
printf("%s w addr val\n", file);
}

int main(int argc, char **argv)
{
int fd;
unsigned char buf[2];

if ((argc != 3) && (argc != 4))
{
print_usage(argv[0]);
return -1;
}

fd = open("/dev/at24cxx", O_RDWR);
if (fd < 0)
{
printf("can't open /dev/at24cxx\n");
return -1;
}

if (strcmp(argv[1], "r") == 0)
{
buf[0] = strtoul(argv[2], NULL, 0);
read(fd, buf, 1);
printf("data: %c, %d, 0x%2x\n", buf[0], buf[0], buf[0]);
}
else if (strcmp(argv[1], "w") == 0)
{
buf[0] = strtoul(argv[2], NULL, 0);
buf[1] = strtoul(argv[3], NULL, 0);
write(fd, buf, 2);
}
else
{
print_usage(argv[0]);
return -1;
}

return 0;
}
>>>>>>> 17c977e6349bfc976796a3b9919e4dfc54798d66
